package com.jn.erp.web.api.student.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.web.api.student.entity.dto.TStudentAppointmentDto;
import com.jn.erp.web.api.student.service.TStudentAppointmentService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/auditionCourse")
public class AuditionCourseController extends BaseWebAuthController {


    @Autowired
    private TStudentAppointmentService tStudentAppointmentService;


    /**
     * 试听列表
     */
    @GetMapping("auditionList")
    public TableDataInfo auditionList(TStudentAppointmentDto tStudentAppointmentDto) {
        startPage();
        tStudentAppointmentDto.setOrgId(Long.parseLong(loginDeptAuth()));
        List<TStudentAppointmentDto> list = tStudentAppointmentService.auditionList(tStudentAppointmentDto);
        return getDataTable(list);
    }

    /**
     取消试听
     */

    @PostMapping("/cancelAudition")
    public AjaxResult cancelAudition (@RequestBody TStudentAppointmentDto tStudentAppointmentDto) {
        // 机构id报读
        tStudentAppointmentDto.setOrgId(Long.parseLong(loginDeptAuth()));
         tStudentAppointmentService.cancelAudition(tStudentAppointmentDto);
        return success("取消试听成功");
    }

    /**
     * 开班试听
     * @param tStudentAppointmentDto
     * @return
     */
    @PostMapping("/startClassAudition")
    public AjaxResult startClassAudition (@RequestBody TStudentAppointmentDto tStudentAppointmentDto) {
        tStudentAppointmentDto.setOrgId(Long.parseLong(loginDeptAuth()));
        tStudentAppointmentService.startClassAudition(tStudentAppointmentDto);
        return success("新建课次成功");
    }

    /**
     * 试听课次列表
     */
    @GetMapping("auditionLessonsList")
    public TableDataInfo auditionLessonsList(TStudentAppointmentDto tStudentAppointmentDto) {
        startPage();
        tStudentAppointmentDto.setOrgId(Long.parseLong(loginDeptAuth()));
        List<TStudentAppointmentDto> list = tStudentAppointmentService.auditionLessonsList(tStudentAppointmentDto);
        return getDataTable(list);
    }


    @RequestMapping("/auditionListExport")
    public void auditionListExport(@RequestBody TStudentAppointmentDto tStudentAppointmentDto, HttpServletResponse response) {
        //1.查询数据
        // 2在读 5历史
        long orgId = Long.parseLong(loginDeptAuth());
        tStudentAppointmentDto.setOrgId(orgId);
        List<TStudentAppointmentDto> list1 = tStudentAppointmentService.auditionList(tStudentAppointmentDto);

        //2.封装成表格
        //参数1：表格标题，参数2：sheet名称
        ExportParams exportParams = new ExportParams(null, "报读情况导出");
        //参数1：表格参数  参数2：实体类  参数3：数据
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, TStudentAppointmentDto.class, list1);
        //3.返回表格
        //设置表格文件名字
        String fileName = "报读情况导出";
        try {
            fileName = URLEncoder.encode(fileName, "UTF8");
            //设置返回数据类型
            response.setHeader("Access-Control-Expose-Headers", "content-disposition");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");

            //将表格输出
            OutputStream os = response.getOutputStream();
            sheets.write(os);
            os.close();
            os.flush();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
